﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.WinKStart.ViewModel;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.WinKStart.Abstract;
using WMSSoft.Lib.WinKStart.Model.MenuConfig;

namespace WMSSoft.WinKStart.ViewModel
{
    public class CContentActionsViewModel : CCommandViewModel
    {
        List<cWinKStartAction> _Data = null;
        cWinKStartAction _SelectedDataItem = null;
        CContentConfig _Config = null;
        bool _CanWorkWithItems = false;

        public CContentActionsViewModel(CContentConfig Config) 
        {
            PropertyChanged += CContentActionsViewModel_PropertyChanged;
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Add"), new CDelegatingCommand(Delegate => this.Cmd_AddItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Edit"), new CDelegatingCommand(Delegate => this.Cmd_EditItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Delete"), new CDelegatingCommand(Delegate => this.Cmd_DeleteItem())));
            _Config = Config;
        }

        public CContentActionsViewModel(CContentConfig Config, List<cWinKStartAction> Data)
        {
            PropertyChanged += CContentActionsViewModel_PropertyChanged;
            _Data = Data;
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Add"), new CDelegatingCommand(Delegate => this.Cmd_AddItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Edit"), new CDelegatingCommand(Delegate => this.Cmd_EditItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Delete"), new CDelegatingCommand(Delegate => this.Cmd_DeleteItem())));

            _Config = Config;
        }

        void CContentActionsViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            try
            {
                if (e.PropertyName == "SelectedDataItem")
                    SetCanWorkWithItems();
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on changed Property '" + e.PropertyName + "'!", Ex, EMessageTypes.Error);
            }
        }

        public CContentActionsViewModel(string ViewName)
        {
            PropertyChanged += CContentActionsViewModel_PropertyChanged;
            base.ViewName = ViewName;
        }

        #region Properties

        public List<cWinKStartAction> Data
        {
            get { return _Data; }
        }

        public cWinKStartAction SelectedDataItem
        {
            get { return _SelectedDataItem; }
            set 
            {
                _SelectedDataItem = value;
                OnPropertyChanged("SelectedDataItem");
            }
        }

        public bool CanWorkWithItems
        {
            get { return _CanWorkWithItems; }
            set
            {
                _CanWorkWithItems = value;
                OnPropertyChanged("CanWorkWithItems");
            }
        }

        public CSingleCommandViewModel Cmd_Add
        {
            get { return m_oCommands[0]; }
        }

        public CSingleCommandViewModel Cmd_Edit
        {
            get { return m_oCommands[1]; }
        }

        public CSingleCommandViewModel Cmd_Delete
        {
            get { return m_oCommands[2]; }
        }

        #endregion

        void SetCanWorkWithItems()
        {
            if (_SelectedDataItem != null)
            {
                CanWorkWithItems = true;
            }
            else CanWorkWithItems = false;
        }

        #region Command Procedures

        public void Cmd_AddItem() 
        {
            try
            {
                System.Windows.Controls.ListView tmpListBox = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_Add.Command).CommandParameter != null)
                {
                    tmpListBox = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_Add.Command).CommandParameter;

                    Setting_NewEditActionViewModel tmpViewModel = new Setting_NewEditActionViewModel(_Config);
                    Settings_NewEditAction tmpNewActionDlg = new Settings_NewEditAction();
                    tmpNewActionDlg.DataContext = tmpViewModel;
                    if (tmpNewActionDlg.ShowDialog() == true)
                    {
                        //TODO: Add Action Item to list
                        if (FindActionElement(((Setting_NewEditActionViewModel)tmpNewActionDlg.DataContext).ActionName) == false) {
                            Data.Add( ((Setting_NewEditActionViewModel)tmpNewActionDlg.DataContext).Data);
                            tmpListBox.Items.Refresh();
                        }

                        //TODO: Search new Item does not already exists before adding
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on Show Add Action Dialog!", Ex, EMessageTypes.Error);
            }
        }

        public void Cmd_EditItem()
        {
            try
            {
                System.Windows.Controls.ListView tmpListBox = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_Edit.Command).CommandParameter != null)
                {
                    tmpListBox = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_Edit.Command).CommandParameter;
                    if (_SelectedDataItem != null)
                    {
                        Setting_NewEditActionViewModel tmpViewModel = new Setting_NewEditActionViewModel(_Config, _SelectedDataItem);
                        Settings_NewEditAction tmpNewActionDlg = new Settings_NewEditAction();
                        tmpNewActionDlg.DataContext = tmpViewModel;
                        if (tmpNewActionDlg.ShowDialog() == true)
                        {
                            //TODO: Add Action Item to list
                            _SelectedDataItem = ((Setting_NewEditActionViewModel)tmpNewActionDlg.DataContext).Data;
                            tmpListBox.Items.Refresh();
                        }
                    }
                    else
                    {
                        System.Windows.MessageBox.Show(LanguageStrings.GetText("Please select a Action Item before edit!"),
                                                       LanguageStrings.GetText("Hint"), System.Windows.MessageBoxButton.OK);
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on Show Edit Action Dialog!", Ex, EMessageTypes.Error);
            }
        }

        public void Cmd_DeleteItem() 
        {
            try
            {
                System.Windows.Controls.ListView tmpListBox = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_Delete.Command).CommandParameter != null)
                {
                    tmpListBox = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_Delete.Command).CommandParameter;
                    if (_SelectedDataItem != null)
                    {
                        string stmpLocalizedMessage = LanguageStrings.GetText("Delete Action '#ACTION#'?");
                        stmpLocalizedMessage = stmpLocalizedMessage.Replace("#ACTION#", _SelectedDataItem.Name);
                        if (System.Windows.MessageBox.Show(stmpLocalizedMessage, LanguageStrings.GetText("Question"),
                                                           System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes)
                        {
                            Data.Remove(_SelectedDataItem);
                            tmpListBox.Items.Refresh();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to Delete Action!", Ex, EMessageTypes.Error);
            }
        }

        #endregion

        bool FindActionElement(string Name)
        {
            var myselAction = from SingleAction in _Data where SingleAction.Name == Name select SingleAction;
            if (myselAction != null && myselAction.Count() > 0)
                return true;
            else
                return false;
        }
    }
}
